<?php
class formActions extends sfActions
{  
  public function executeMedia_set_order(sfWebRequest $request)
  {
    $order = $request->getParameter('order');
    $table = $request->getParameter('table');
    
    $q = Doctrine_Query::create()->update($table);
    
    foreach ($order as $key => $value)
    {
      $q->set('position', $key)->where('id = ?', $value)->execute();
    }

    return sfView::NONE; 
  }  
  
  public function executeMedia_delete_one(sfWebRequest $request)
  {
    $table = $request->getParameter('table');
    
    $id = $request->getParameter('id');

    $file = Doctrine_Core::getTable($table)->find($id)->toArray();
    
    $q = Doctrine_Query::create()->from($table)->select('id')->where('name = ?', $file['name'])->andWhere('extension = ?', $file['extension']);
    
    if($q->count() == 1)
    {
      cmsFile::delete($file, $table);
    }
    
    Doctrine_Query::create()->delete($table)->where('id = ?', $id)->execute();
    
    return sfView::NONE; 
  }
  
    public function executeMedia_description_window(sfWebRequest $request)
    {
      $this->fileId = $request->getParameter('fileId');
      
      $this->fieldId = $request->getParameter('fieldId');
      
      $this->field = Doctrine_Core::getTable('cmsField')->find($this->fieldId)->toArray();
      
      $this->description = Doctrine_Query::create()->from($this->field['tab'])->select('description')->where('id = ?', $this->fileId)->fetchOne()->getDescription();      
    }
    
    public function executeMedia_description_save(sfWebRequest $request)
    {
      sfProjectConfiguration::getActive()->loadHelpers('Partial');
      
      $fileId = $request->getParameter('fileId');
      
      $fieldId = $request->getParameter('fieldId');
      
      $description = $request->getParameter('description');
      
      $field = Doctrine_Core::getTable('cmsField')->find($fieldId)->toArray();
      
      Doctrine_Query::create()->update($field['tab'])->set('description', '?',  $description)->where('id = ?', $fileId)->execute();
      
      $file = Doctrine_Core::getTable($field['tab'])->find($fileId)->toArray();
          
      echo get_component('field', 'images', array('field'=>$field, 'request'=>$request, 'file'=>$file));
     
      return sfView::NONE;
      
    }
}